oo::class create ProjectMenu {
    superclass AppMenu
    variable Menu Label NotebookObj Notebook \
	PopupNewProjectDialog \
	PopupConfigureProjectDialog

    constructor {menubar} {
        next $menubar project
        set PopupNewProjectDialog [PopupNewProjectDialog new .project  $::ProjectLocation]
	set PopupConfigureProjectDialog [PopupConfigureProjectDialog new .config]
    }
    
    method createMenu {} {
        foreach row {
            {command {New {} 0 createNewProject}}
            {command {Config {} 0 configureProject}}
	    {command {Close {} 3 closeProject}}
	    {command {Remove {} 0 removeProject}}
	    {command {Delete {} 0 deleteProject}}
	    {separator {}}
	    {command {Test {} 0 testProject}}
        } {
            my createItem $row
        }
    }
    
    method createNewProject {} {
	variable PopupNewProjectDialog
        set row [$PopupNewProjectDialog popup "Create New Project"]
        if {$row != {}} {
            my GenerateNewProject $row
        }
    }
    
    method GenerateNewProject {row} {
	set location [lindex $row 2]
	set name [lindex $row 0]
        set dir [file join $location $name]
        file mkdir $dir
        cd $dir
        foreach d {src lib test} {file mkdir $d}
        my WriteProjectFile $dir {*}$row
    }

    method WriteProjectFile {dir name packages location description version} {
	set fh [open [file join $dir project.tcl] w]
        puts $fh "#PROJECT:$name\nset version $version\nset description \"$description\"\nset dependencies \[list $packages\]\nset main main.tcl"
        close $fh
        
        set mainsrc [file join $dir src main.tcl]
        set fh [open $mainsrc w]
        puts $fh "# IMPORT \n\n# ARGS\n\n# MAIN\n\n# RUN\nif {\$argv0 == \[info script\]} {\n\n}"
        close $fh
        
        set fh [open [file join $dir test main.tcl] w]
        puts $fh "# IMPORT\n\n# ARGS\n\n# TEST\nnamespace eval test[string toupper $name 0] {\n\n}"
        close $fh
        
        $NotebookObj openFile [$NotebookObj nextTabid [$Notebook index current]] $mainsrc 
    }

    method getProjectView {} {
	set pv [lindex [info class instances ::ProjectView] 0]
	set lbf [set [info object namespace $pv]::Lbf]
	return [list $pv $lbf]
    }

    method configureProject {} {
	foreach {pv lbf} [my getProjectView] {}
	if {[set e [$lbf selection]] == {}} { return }

	set row [$PopupConfigureProjectDialog popup "Configure Project" [lindex $e 0]]
	if {$row != {}} {
	    foreach {name _ location _ _ _} $row {}
	    set dir [file join $location $name]
	    my WriteProjectFile $dir {*}$row
	    my updateProjectInNotebook [file join $dir project.tcl]
	}
    }
    
    method updateProjectInNotebook {file} {
	foreach c [info class instances ::FileContainer] {
	    set f [set [info object namespace $c]::File] {
		if {$f == $file} {
		    $NotebookObj importFile $c $file
		    break
		}
	    }
	}
    }
}